﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Core;
using System.Drawing;
using Silvermoon.Shapes;

using Silvermoon.Utils;
using Silvermoon.Images;

using Silvermoon.UI;
using Silvermoon.Windows;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Provides a modal dialog.
    /// </summary>
    public class Dialog : DialogBase
    {

        private ModalBackground modalBackground;
        private int modalOpacity = 128;

        private bool isModal;

        public Dialog()
            : base()
        {
            ModalColor = Color.Black;
            TextColor = SysColors.DialogTextColor;
        }

        protected override void Dispose(bool dispose)
        {
            if (modalBackground != null) modalBackground.Dispose();
            base.Dispose(dispose);
        }

        protected override Shape CreateDefaultBackground()
        {
            Color color = SysColors.DialogColor;
            return new ContextMenuShape { Color = color };
        }

        protected override Shape CreateDefaultChrome()
        {
            return new BackgroundShape { Texture = SysImages.TextBoxChrome, Style = RenderStyle.Edged };
        }

        /// <summary>
        /// Get or sets the alpha value for the background when the dialog is shown modal.
        /// </summary>
        public int ModalOpacity
        {
            get { return modalOpacity; }
            set
            {
                modalOpacity = ValidateOpacity(value);
            }
        }

        /// <summary>
        /// Gets or sets the color for the background when this dialog is shown modal.
        /// </summary>
        public Color ModalColor { get; set; }

        protected override void OnInitialized()
        {
            base.OnInitialized();
        }

        protected override void ApplyStyle()
        {
            base.ApplyStyle();
            if (ChromeColor.IsEmpty) ChromeColor = ColorUtil.FromArgb(160, 255, 255, 255);
        }

        public override void Show(VisualState showState)
        {
            isModal = false;
            base.Show(showState);
        }

        public virtual void ShowModal()
        {
            isModal = true;
            if (modalBackground == null)
            {
                modalBackground = new ModalBackground { Opacity = modalOpacity };
            }
            modalBackground.Color = ModalColor;
            Screen.Current.Attach(modalBackground);
            base.Show();
        }

        protected override void OnHidden()
        {
            if (modalBackground != null)
            {
                Screen.Current.Detach(modalBackground);
                modalBackground.Dispose();
                modalBackground = null;
            }
            base.OnHidden();
            OnClosed(Result);
        }

        protected virtual void OnClosed(DialogResult result)
        {
            if (Closed != null) Schedule(() => Closed(this, new DialogEventArgs { Result = result }));
        }

        protected override void OnVisibilityChanged(float oldValue)
        {
            if (isModal && modalBackground != null)
            {
                modalBackground.Opacity = (int)(ModalOpacity * Visibility);
            }
            base.OnVisibilityChanged(oldValue);
        }

        /// <summary>
        /// Occurs when the dialog is closed.
        /// </summary>
        public event DialogResultEventHandler Closed;


    }

}
